package com.tjetc.service.impl;

import com.tjetc.dao.DclassesDao;
import com.tjetc.dao.DoctorDao;
import com.tjetc.dao.NclassesDao;
import com.tjetc.dao.NurseDao;
import com.tjetc.dao.impl.DclassesDaoImpl;
import com.tjetc.dao.impl.DoctorDaoImpl;
import com.tjetc.dao.impl.NclassesDaoImpl;
import com.tjetc.dao.impl.NurseDaoImpl;
import com.tjetc.domain.Dclasses;
import com.tjetc.domain.DocNurClasses;
import com.tjetc.domain.Doctor;
import com.tjetc.domain.DorClasses;
import com.tjetc.service.DclassesService;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DclassesServiceImpl implements DclassesService{
    private DclassesDao dclassesDao = new DclassesDaoImpl();
    private DoctorDao doctorDao = new DoctorDaoImpl();
    private NurseDao nurseDao = new NurseDaoImpl();
    private NclassesDao nclassesDao = new NclassesDaoImpl();
    @Override
    public boolean addDoc(Dclasses dclasses) {
        try {
            dclassesDao.addDoc(dclasses);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int getDocCount(int did) {
        try {
            return dclassesDao.getDocCount(did);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<DocNurClasses> listClasses(String date) {
        try {
            List<DocNurClasses> list = dclassesDao.listClasses(date);
            /*Set<String> dNames = new HashSet<>();
            Set<String> nNames = new HashSet<>();
            for (DocNurClasses classes : list) {
                if (classes.getNids() != null && classes.getDids() != ""){
                    String[] dids = classes.getNids().split(",");
                    for (String did : dids) {
                        Doctor doctor = doctorDao.getById(Integer.valueOf(did));
                        dNames.add(doctor.getUsername());
                    }
                }
                if (classes.getNids() != null && classes.getNids() != ""){
                    String[] nids = classes.getNids().split(",");
                    for (String nid : nids) {
                        Nurse nurse = nurseDao.getById(Integer.valueOf(nid));
                        nNames.add(nurse.getUsername());
                    }
                }
                classes.setDNames(dNames);
                classes.setNNames(nNames);
            }*/
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<DorClasses> listDorClasses(PageInfo<DorClasses> pageInfo) {
        try {
            List<DorClasses> list = dclassesDao.listDorClasses(pageInfo);
            for (DorClasses classes : list) {
                List<String> dNames = new ArrayList<>();
                String[] split = classes.getDids().split(",");
                for (String s : split) {
                    Doctor doctor = doctorDao.getById(Integer.valueOf(s));
                    dNames.add(doctor.getUsername());
                }
                classes.setNames(dNames);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount() {
        try {
            return dclassesDao.getCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Dclasses findByIdandDate(int id, String dayOfWeekName) {
        try {
            return dclassesDao.findByIdandDate(id,dayOfWeekName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean setqiandao(Dclasses dclasses) {
        try {
            return dclassesDao.setqiandao(dclasses)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean clearClasses() {
        try {
            Db.startTransaction();
            //清空医生排班表
            dclassesDao.clearClasses();
            //清空护士排班表
            nclassesDao.clearNclasses();
            Db.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        } finally {
            Db.close();
        }
        return false;
    }


}
